/*
 * ElementInfoPanel.cpp
 *
 *  Created on: 27-12-2010
 *      Author: Przemek
 */

#include "customUI/ElementInfoPanel.hpp"
#include "world/visitors/InfoPanelCreatorVisitor.hpp"
using std::string;

const string ElementInfoPanel::NONE_SELECTED = "<none selected>";

ElementInfoPanel::ElementInfoPanel(const Dimension& parentSize) :
	selected(0), elementInfoPanel(0), parentWidth(parentSize.getWidth())
{
	static const float lineHeight = 20;
	const Dimension fullLineSize(parentSize.getWidth(), lineHeight);
	const Dimension halfLineSize(fullLineSize.getWidth() / 2, fullLineSize.getHeight());
	setSize(parentSize);
	setPosition(Vector(0.0f, parentSize.getHeight()));

	float y = 0.0f;
	Label* label = new Label("___Element info___", GLColor::RED);
	label->setPosition(Vector(0.0f, y));
	label->setSize(fullLineSize);
	add(label);

	y += lineHeight;
	label = new Label("Selected:   ");
	label->setPosition(Vector(0.0f, y));
	label->setSize(halfLineSize);
	add(label);

	nameLabel = new Label(NONE_SELECTED);
	nameLabel->setPosition(Vector(halfLineSize.getWidth(), y));
	nameLabel->setSize(halfLineSize);
	add(nameLabel);

	elementInfoPanelY = y + lineHeight;
}

void ElementInfoPanel::updateFromListener(SelectElementMouseListener* listener)
{
	if(selected != listener->getSelected())
	{
		nameLabel->setText(NONE_SELECTED);
		selected = listener->getSelected();
		if(elementInfoPanel != 0)
		{
			remove(elementInfoPanel);
			elementInfoPanel = 0;
		}
		if(listener->anySelected())
		{
			InfoPanelCreatorVisitor infoPanelCreatorVisitor = InfoPanelCreatorVisitor(parentWidth);
			selected->accept(infoPanelCreatorVisitor);
			elementInfoPanel = infoPanelCreatorVisitor.getPanel();
			elementInfoPanel->setPosition(Vector(0.0f, elementInfoPanelY));
			add(elementInfoPanel);
			nameLabel->setText(elementInfoPanel->getSelectionName());
		}
	}
	if(elementInfoPanel != 0)
	{
		elementInfoPanel->tryUpdateFromSelection();
	}
}

